package top.xcxdl.rural.controller.admin;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import top.xcxdl.rural.common.Res;
import top.xcxdl.rural.entity.RuralCourse;
import top.xcxdl.rural.entity.RuralExamination;
import top.xcxdl.rural.service.IRuralCourseService;
import top.xcxdl.rural.service.IRuralExaminationService;
import top.xcxdl.rural.utils.ImageAddress;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Ding
 * @since 2022-05-08
 */
@RestController
@RequestMapping("/admin/course")
public class BackRuralCourseController {

    @Resource
    private IRuralCourseService service;

    @Resource
    private IRuralExaminationService examinationService;

    @GetMapping("/getList")
    @ApiOperation("获取课程列表")
    @ResponseBody
    public Res<Object> getList(
            @RequestParam(name = "page_no") Integer pageNo,
            @RequestParam(name = "page_size") Integer pageSize,
            @RequestParam(name = "title", required = false) String key
    ) {
        PageInfo<RuralCourse> pageInfo = service.getList(pageNo, pageSize, key);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return Res.success(jsonObject);
    }

    @GetMapping("/getDetail")
    @ApiOperation("获取课程详情（章节和课节）")
    @ResponseBody
    public Res<RuralCourse> getDetail(
            @RequestParam(name = "course_id") Integer courseId
    ) {
        //获取课程详情
        RuralCourse course = service.getDetail(courseId);
        //获取课程章节和课节
        List<RuralCourse> chapters = service.getChapterAndLessons(courseId);
        course.setChildren(chapters);
        //计算课程的章节数和课节数
        course.setChapterNums(chapters.size());
        chapters.forEach(chapter -> {
            if (chapter.getChildren() != null && chapter.getChildren().size() > 0) {
                chapter.getChildren().forEach(lesson -> {
                    lesson.setCourseResourceUrl(ImageAddress.getAllPath(lesson.getCourseResourceUrl()));
                });
                course.setLessonNums(0);
                course.setLessonNums(course.getLessonNums() + chapter.getChildren().size());
            }
        });
        return Res.success(course);
    }

    @PostMapping("/doEdit")
    @ApiOperation("新增 or 编辑课程")
    @ResponseBody
    public Res<Object> doEdit(@RequestBody RuralCourse course) {
        int ret = service.doEdit(course);
        return ret > 0 ? Res.success("操作成功") : Res.error("操作失败");
    }

    @PostMapping("/doDelete")
    @ApiOperation("删除课程")
    @ResponseBody
    public Res<Object> doDelete(@RequestBody JSONObject jsonObject) {
        //获取id集合
        List<Integer> ids = (List<Integer>) jsonObject.get("ids");
        int ret = service.doDelete(ids);
        return ret > 0 ? Res.success("删除成功") : Res.error("操作失败");
    }

    @GetMapping("/getExaminationList")
    @ApiOperation("获取试卷列表")
    @ResponseBody
    public Res<List<RuralExamination>> getExaminationList(
            @RequestParam(name = "title", required = false) String key) {
        QueryWrapper<RuralExamination> wrapper = new QueryWrapper<>();
        wrapper.like("exam_name", key)
        .orderByDesc("exam_id");
        return Res.success(examinationService.list(wrapper));
    }
}
